package com.ljkj.cordial.chat.util;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.widget.SingleCommonDialog;
import com.nic.view.util.SharedUtils;

/**
 * 阅后即焚工具类，key需要加userId的话再补
 */
public class ReadFinishCycTimeUtil {
    private static class Builder {
        public static ReadFinishCycTimeUtil INSTANCE = new ReadFinishCycTimeUtil();
    }

    public static ReadFinishCycTimeUtil getInstance() {
        return Builder.INSTANCE;
    }

//    TimeChoosePop timeChoosePop;

    /**
     * 选择时间
     *
     * @param context
     * @param key
     * @param tvTime
     */
    public void showPop(Context context, String key, TextView tvTime) {
//        if (timeChoosePop == null) {
//            timeChoosePop = new TimeChoosePop(context);
//            timeChoosePop.setOnConfirmListener(new TimeChoosePop.OnConfirmListener() {
//                @Override
//                public void onConfirm(int time) {
//                    tvTime.setText(time + "s");
//                    SharedUtils.put(context, key, time);
//                }
//            });
//            timeChoosePop.getPopupWindow().setOnDismissListener(new PopupWindow.OnDismissListener() {
//                @Override
//                public void onDismiss() {
//                    timeChoosePop = null;
//                }
//            });
//        }
//        if (!timeChoosePop.getPopupWindow().isShowing()) {
//            timeChoosePop.showBottom(((Activity) context).getWindow().getDecorView());
//        }
        int sysReadFinishCycTime = (int) SharedUtils.get(context, key, 5 * 60);
        new SingleCommonDialog(context)
                .setTitle("阅后即焚设置")
                .setSelected(getReadTimePosition(sysReadFinishCycTime))
                .setReadFinishData()
                .setLeftSubmit("取消", null).setRightSubmitGray("确认", new SingleCommonDialog.OnSingleItemClickListener() {
            @Override
            public void onClick(View view, int position, String value) {
                tvTime.setText(getReadFinishTime(position));
                SharedUtils.put(context, key, getReadTime(position));
            }
        }).show();

    }

    private int getReadTime(int position) {
        if (position == 0) {
            return 5 * 60;
        } else if (position == 1) {
            return 10 * 60;
        } else if (position == 2) {
            return 30 * 60;
        } else if (position == 3) {
            return 60 * 60;
        }
        return 30;
    }

    private int getReadTimePosition(int time) {
        if (time == 5 * 60) {
            return 0;
        } else if (time == 10 * 60) {
            return 1;
        } else if (time == 30 * 60) {
            return 2;
        } else if (time == 60 * 60) {
            return 3;
        }
        return 0;
    }

    public String getReadFinishTime(int position) {
        String[] str = ChatApp.getInstance().getResources().getStringArray(R.array.array_read_finish_clear);
        return str[position];
    }

    /**
     * 初始设置
     *
     * @param context
     * @param key
     * @param tvTime
     */
    public void setReadFinishCycTime(Context context, String key, TextView tvTime) {
        int sysReadFinishCycTime = (int) SharedUtils.get(context, key, 5 * 60);
        tvTime.setText(sysReadFinishCycTime < 60 ? (sysReadFinishCycTime + "s") : (sysReadFinishCycTime / 60 + "分钟"));
    }

    public int getReadFinishCycTime(Context context, String key) {
        return (int) SharedUtils.get(context, key, 5 * 60);
    }

    /**
     * 初始设置
     *
     * @param context
     * @param key
     * @param swReadFinishClear
     * @param llReadFinishClear
     */
    public void setReadFinishCycChangeListener(Context context, String key, SwitchCompat swReadFinishClear, ViewGroup llReadFinishClear) {
        swReadFinishClear.setChecked(isReadFinish(context, key));
        TextView textView = (TextView) llReadFinishClear.getChildAt(0);
        setTextViewColor(context, textView, swReadFinishClear.isChecked());
        swReadFinishClear.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                try {
                    SharedUtils.put(context, key, isChecked);
                    if (isChecked) {
                        llReadFinishClear.setEnabled(true);
                    } else {
                        llReadFinishClear.setEnabled(false);
                    }
                    setTextViewColor(context, textView, isChecked);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void setReadFinishCycChangeListener(Context context, String key, CheckBox swReadFinishClear, ViewGroup llReadFinishClear) {
        swReadFinishClear.setChecked(isReadFinish(context, key));
        llReadFinishClear.setEnabled(isReadFinish(context, key));
        TextView textView = (TextView) llReadFinishClear.getChildAt(0);
        setTextViewColor(context, textView, swReadFinishClear.isChecked());
        swReadFinishClear.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                try {
                    SharedUtils.put(context, key, isChecked);
                    if (isChecked) {
                        llReadFinishClear.setEnabled(true);
                    } else {
                        llReadFinishClear.setEnabled(false);
                    }
                    setTextViewColor(context, textView, isChecked);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void setTextViewColor(Context context, TextView tv, boolean isCheck) {
        if (isCheck) {
            tv.setTextColor(Color.parseColor("#000000"));
        } else {
            tv.setTextColor(ContextCompat.getColor(context, R.color.gray_500));
        }
    }

    public boolean isReadFinish(Context context, String key) {
        return (boolean) SharedUtils.get(context, key, true);
    }

    public int getReadFinish(Context context, String key) {
        return (int) SharedUtils.get(context, key, 5 * 60);
    }
}
